﻿@using Smartstore.Web.Rendering.Menus
@using Smartstore.Collections
@using Smartstore.Core.Content.Menus

@model MenuModel

@{
    var root = Model.Root;
    if (root == null)
    {
        return;
    }
}

<ul class="navbar-nav">
    @{
        foreach (var node in Model.Root.Children)
        {
            var item = node.Value;

            if (!item.Visible)
            {
                continue;
            }

            var imageUrl = item.ImageUrl;
            if (!imageUrl.HasValue())
            {
                imageUrl = "~/admin/images/ap-default-white.png";
            }

            var itemText = node.GetItemText(T);
            var itemUrl = GetNodeLink(node);

            if (ViewBag.SelectedMenuItem == itemText)
            {
                //item.Selected = true;
            }

        <li data-id="nav-@item.Id" class="nav-item@(node.HasChildren ? " dropdown" : "")@(item.Id == "dashboard" ? " dropdown d-none d-md-block" : "")">
            <a class="nav-link@(node.HasChildren ? " dropdown-toggle" : "")" href="@itemUrl" attr-data-toggle='(node.HasChildren, "dropdown")'>
                @if (item.Icon.HasValue())
                {
                    <i class="icm icm-@item.Icon navbar-icon"></i>
                }
                else
                {
                    <img class="sm-icon" src="@Url.Content(imageUrl)" alt="" />
                }
                <span class="navbar-label d-none d-lg-block">@itemText</span>
            </a>
            @if (node.HasChildren)
            {
                await CreateDropdown(node.Children, true);
            }
        </li>
        }
    }
</ul>

@{
    string GetNodeLink(TreeNode<MenuItem> node)
    {
        if (node.HasChildren)
        {
            return "javascript:void(0)";
        }

        return node.Value.GenerateUrl(this.ViewContext);
    }

    async Task CreateDropdown(IEnumerable<TreeNode<MenuItem>> nodes, bool isRoot)
    {
        var isFirst = true;
        var hasIcons = nodes.Any(x => x.Value.Icon.HasValue());

        <ul class="dropdown-menu">
            @foreach (var node in nodes)
            {
                var item = node.Value;

                if (!item.Visible)
                {
                    continue;
                }

                var itemText = node.GetItemText(T);

                if (item.IsGroupHeader)
                {
                    <li sm-if="!isFirst" class="dropdown-divider"></li>
                    <li sm-if="@(item.Text.HasValue() && item.Text != "[SKIP]")" data-id="@item.Id" class="dropdown-header h6">@itemText</li>
                    isFirst = false;
                    continue;
                }

                <li data-id="@item.Id" attr-class='(node.HasChildren, "dropdown-group")'>
                    <a class="dropdown-item" href="@GetNodeLink(node)">
                        @if (hasIcons)
                        {
                            if (item.Icon.HasValue())
                            {
                                if (item.IconLibrary == "bi")
                                {
                                    <bootstrap-icon name="@item.Icon" class="dropdown-icon bi-fw" attr-class='@(item.IconClass.HasValue(), item.IconClass)' />
                                }
                                else
                                {
                                    <i class="@item.Icon" attr-class='@(item.IconClass.HasValue(), item.IconClass)'></i>
                                }
                            }
                            else
                            {
                                <i class="bi bi-fw"></i>
                            }
                        }
                        @itemText
                    </a>
                    @if (node.HasChildren)
                    {
                        await CreateDropdown(node.Children, false);
                    }
                </li>

                isFirst = false;
            }
        </ul>
    }
}